﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using eap.share;
using System.Text.RegularExpressions;
using System.Text;

namespace zsproject.page
{
	public class Timetable : System.Web.UI.Page
	{
		//存放要传递到aspx文件显示的数据
		public System.Collections.Hashtable output=new System.Collections.Hashtable();
		private string method="";

		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			//---------------------------------------------------------------
			Response.AddHeader("Pragma","No-cache");
			Response.AddHeader("Cache-Control", "no-cache");
			Response.Expires=0;
			//---------------------------------------------------------------

			//读取当前方法
			string page=Req.getPara(Request,"page");
			method=Req.getPara(Request,"method");

			//初始化系统(基本参数，配置参数，数据库连接参数)
			AdminLib.init(Request,Response,Server);
			//检查用户是否登录，是否有权限
			AdminUser user = (AdminUser)Session["_user"];

			if(user==null || !user.isLogin()){ Response.Redirect("../admin/index.aspx?page=unlogin"); Response.End() ; }
			if(user.isRelogin(Request)){ Response.Redirect("../admin/index.aspx?page=relogin"); Response.End() ; }
			//没有菜单的,特殊链接可以不要
			if(!user.getRight(Request)){ Response.Redirect("../admin/index.aspx?page=erraccess"); Response.End() ; }
			if(!user.isWarning()){ Response.Redirect("../admin/index.aspx?page=warning"); Response.End(); }
			//资源权限
			output.Add("actlist",DAO.sqlValueList("SELECT * FROM  dbo.getRightId("+user["id"]+")")); //保存用户有权限的操作	

			if(page=="List") List(Request,Response,Session,output);
			if(page=="List_action"){List_action(Request,Response,Session,output);}
			if(page=="Form") Form(Request,Response,Session,output);			
			if(page=="Form_action"){Form_action(Request,Response,Session,output);}
		}

	 
		private void List(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			string group_id=Sys.toSQLText(Req.getPara(Request,"group_id"));
			string cond2 = " 1=1 ";      //附加的查询条件

			//查询属于下属
			if(group_id=="")
			{
				//查询所有管辖区域的企业
				group_id= user["dept_id"];				
			}

			// 设置参数			
			string deforder="";   //缺省的排序条件
			string orderTable="";  //排序字段缺省对应的表名，空表示缺省(主要是因为access数据库查询用join时不指定表名容易报错)
			string table="v_timetable tb";  //要查询的表名
			string flist = "";    //可以检索的字段， 用'+'分格
			string sfield = "tb.*"; //要在select 语句中查询的字段,空或*表示所有
			string search_title="根据录入的关键字进行检索";  //查询提示
			//标题
			string title="";
			//查询数据库
			AdminList lst=new AdminList(Request,AdminLib.getSimpleRequestURL(Request)+"?page="+Req.getPara(Request,"page"),deforder,user["cfg_list"]);
			System.Data.DataRowCollection list=lst.getList(table,flist,cond2,sfield,orderTable);
			//保存结果
			output.Add("title",title);
			output.Add("search_title",search_title);
			output.Add("list",list);
			output.Add("lst",lst);
		}//end method

		private void List_action(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			int errcode=0;
			string errmsg="";
			string sql = "",message="",strMenu="信息";
			string action=Req.getPara(Request,"action");

			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);

			//检查输入的内容
			//if(title=="") errmsg=errmsg+"标题不能为空。";

			//删除某条记录/删除选中的记录
			if(action=="delete")
			{
				if(d==null||d=="") 
				{
					d=AdminLib.getSelectedCheckbox(Request); //读取选中的记录
				}
            
				if(d==null||d=="")
				{ 
					Response.Write(user.getActionAlarm("您还未选择记录，请选择。")); 
					return; 
				} 
				errcode=errcode+DAO.sqlExec("delete from t_timetable where id in ("+d+")");
				
				message=strMenu+"(d="+d+")，删除"+(errcode==0?"成功":"出错"+errmsg)+"。";
			}

			if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }

			//Response.Write("<script>alert('"+message+"');</script>");
			if(errcode!=0) message=message+"(errcode="+errcode+")";
			if(errcode!=0) user.saveLog(Request,message); //保存日志
			string reload=errcode==0?Req.getPara(Request,"reload").Replace("{$id}",d):null; //如果发生了错误，则不需要重新刷新
			Response.Write(user.getActionString(Session,errcode,reload,message)); //显示结果 
		} 

		private void Form(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			
			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);

			System.Data.DataRow map=DAO.sqlRow("select * from t_timetable where id ='"+d+"'");			


			string list_id=Sys.toSQLText(Req.getPara(Request,"list_id"));
			System.Data.DataRow map_list=DAO.sqlRow("select * from t_timetable_list where id ='"+list_id+"'");		
			/*-------------------------------------附件信息---------------------------------------*/
			/*
			if(map!=null && map["accessory_ids"]!=null && map["accessory_ids"].ToString()!="")
			{
				string sql="select * from tbzs_accessory where id in("+map["accessory_ids"]+")";
				DataRowCollection listAtt = DAO.sqlList(sql);//附件信息
				output.Add("listAtt",listAtt);
			}
			*/
			/*-------------------------------------附件信息---------------------------------------*/
			//保存结果
			output.Add("map",map);		 
			output.Add("map_list",map_list);		 
		}//end method 

		private void Form_action(System.Web.HttpRequest Request, System.Web.HttpResponse Response,System.Web.SessionState.HttpSessionState Session,System.Collections.Hashtable output)
		{
			AdminUser user = (AdminUser)Session["_user"];
			string sql = "",message="",strMenu="信息";
			string errmsg="";
			int errcode=0;
			string action=Req.getPara(Request,"action");

			string d=Sys.toSQLText(Req.getPara(Request,"d"));
			if(d!="") d=""+Sys.toInteger(d);
			//获取页面参数
			string id=Sys.toSQLText(Req.getPara(Request,"id"));
			string t_specialty_id=Sys.toSQLText(Req.getPara(Request,"t_specialty_id"));
			string t_grade_id=Sys.toSQLText(Req.getPara(Request,"t_grade_id"));
			string t_class_id=Sys.toSQLText(Req.getPara(Request,"t_class_id"));
			string t_year_id=Sys.toSQLText(Req.getPara(Request,"t_year_id"));
			string t_semester_id=Sys.toSQLText(Req.getPara(Request,"t_semester_id"));
			string weeks=Sys.toSQLText(Req.getPara(Request,"weeks"));
			string add_time=Sys.toSQLText(Req.getPara(Request,"add_time"));
			string add_user=Sys.toSQLText(Req.getPara(Request,"add_user"));

			//检查输入的内容
			//if(title=="") errmsg=errmsg+"标题不能为空。";
			if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }		
		
			
			if(action=="edit")
			{
				if(d=="")
				{
					//新增
					sql=@"INSERT INTO t_timetable
					(						
						[t_specialty_id] ,
						[t_grade_id] ,
						[t_class_id] ,
						[t_year_id] ,
						[t_semester_id] ,
						[weeks] ,
						[add_time] ,
						[add_user] 
					)
					VALUES
					(
						'"+t_specialty_id+
						"','"+t_grade_id+
						"','"+t_class_id+
						"','"+t_year_id+
						"','"+t_semester_id+
						"','"+weeks+
						"','"+add_time+
						"','"+add_user+
							"');Select @@IDENTITY";
					d = DAO.sqlValue(sql).ToString();

					strMenu=strMenu+"(d="+d+")新增";
				} 
				else
				{	
					//修改
					sql=@"UPDATE t_timetable
						SET
						[t_specialty_id] ='"+t_specialty_id+
						"',[t_grade_id]='"+t_grade_id+
						"',[t_class_id]='"+t_class_id+
						"',[t_year_id]='"+t_year_id+
						"',[t_semester_id]='"+t_semester_id+
						"',[weeks]='"+weeks+
						"',[add_time]='"+add_time+
						"',[add_user]='"+add_user+
						"' where id='"+d+"'";
					errcode=DAO.sqlExec(sql);

					strMenu=strMenu+"(d="+d+")修改";
				}
				message=strMenu+(errcode==0?"成功":"出错")+"。";
			
			}		
	

			
			string list_id=Sys.toSQLText(Req.getPara(Request,"list_id"));

			if(action=="save")
			{
				
				


				
				string t_timetable_id=Sys.toSQLText(Req.getPara(Request,"t_timetable_id"));
				string t_teacher_id=Sys.toSQLText(Req.getPara(Request,"t_teacher_id"));
				string classroom_time_sections=Sys.toSQLText(Req.getPara(Request,"classroom_time_sections"));
				string classroom_time_week=Sys.toSQLText(Req.getPara(Request,"classroom_time_week"));
				string t_course_id=Sys.toSQLText(Req.getPara(Request,"t_course_id"));
				string classroom=Sys.toSQLText(Req.getPara(Request,"classroom"));
				string remark=Sys.toSQLText(Req.getPara(Request,"remark"));

				System.Data.DataRow map_temp=null;
				string sql_temp="";
				if(list_id=="")
				{
					sql_temp=string.Format(@"select * from v_timetable_list
									where  t_year_id='{0}' and t_semester_id='{1}' and classroom_time_sections='{2}' 
									and classroom_time_week='{3}' and classroom='{4}'"
									,t_year_id,t_semester_id,classroom_time_sections,classroom_time_week,classroom);
					map_temp=DAO.sqlRow(sql_temp);
					if(map_temp!=null)
					{
						errmsg=errmsg+"在"+map_temp["classroom_time_week"].ToString()+"第"+map_temp["classroom_time_sections"].ToString()+"节"+map_temp["grade_name"].ToString()+map_temp["class_name"].ToString()+"已经安排了教室"+map_temp["t_teacher_id"].ToString();
					}

					sql_temp=string.Format(@"select * from v_timetable_list
									where  t_year_id='{0}' and t_semester_id='{1}' and classroom_time_sections='{2}' 
									and classroom_time_week='{3}' and t_teacher_id='{4}'"
						,t_year_id,t_semester_id,classroom_time_sections,classroom_time_week,t_teacher_id);
					map_temp=DAO.sqlRow(sql_temp);
					if(map_temp!=null)
					{
						errmsg=errmsg+"在"+map_temp["classroom_time_week"].ToString()+"第"+map_temp["classroom_time_sections"].ToString()+"节"+map_temp["grade_name"].ToString()+map_temp["class_name"].ToString()+"已经安排了教师"+map_temp["teacher_name"].ToString();			
					}


				}
				else
				{
					sql_temp=string.Format(@"select * from v_timetable_list
									where  t_year_id='{0}' and t_semester_id='{1}' and classroom_time_sections='{2}' 
									and classroom_time_week='{3}' and classroom='{4}' and t_timetable_id!='{5}'"
									,t_year_id,t_semester_id,classroom_time_sections,classroom_time_week,classroom,d);
					map_temp=DAO.sqlRow(sql_temp);
					if(map_temp!=null)
					{
						errmsg=errmsg+"在"+map_temp["classroom_time_week"].ToString()+"第"+map_temp["classroom_time_sections"].ToString()+"节"+map_temp["grade_name"].ToString()+map_temp["class_name"].ToString()+"已经安排了教室"+map_temp["classroom"].ToString();				
					}


					sql_temp=string.Format(@"select * from v_timetable_list
									where  t_year_id='{0}' and t_semester_id='{1}' and classroom_time_sections='{2}' 
									and classroom_time_week='{3}' and t_teacher_id='{4}' and t_timetable_id!='{5}'"
						,t_year_id,t_semester_id,classroom_time_sections,classroom_time_week,t_teacher_id,d);
					map_temp=DAO.sqlRow(sql_temp);
					if(map_temp!=null)
					{
						errmsg=errmsg+"在"+map_temp["classroom_time_week"].ToString()+"第"+map_temp["classroom_time_sections"].ToString()+"节"+map_temp["grade_name"].ToString()+map_temp["class_name"].ToString()+"已经安排了教师"+map_temp["teacher_name"].ToString();						
					}

				}

				
				//if(title=="") errmsg=errmsg+"标题不能为空。";
				if(errmsg!=""){ Response.Write(user.getActionAlarm(errmsg)); return; }		


				if(list_id=="")
				{
					//新增
					sql=@"INSERT INTO t_timetable_list
					(						
						[t_timetable_id] ,
						[t_teacher_id] ,
						[classroom_time_sections] ,
						[classroom_time_week] ,
						[t_course_id] ,
						[classroom] ,
						[remark] ,
						[add_time] ,
						[add_user] 
					)
					VALUES
					(
						'"+d+
						"','"+t_teacher_id+
						"','"+classroom_time_sections+
						"','"+classroom_time_week+
						"','"+t_course_id+
						"','"+classroom+
						"','"+remark+
						"','"+add_time+
						"','"+add_user+
						"');Select @@IDENTITY";
					list_id = DAO.sqlValue(sql).ToString();

					strMenu=strMenu+"(d="+d+")新增";
				} 
				else
				{	
					//修改
					sql=@"UPDATE t_timetable_list
						SET
						[t_timetable_id] ='"+d+
						"',[t_teacher_id]='"+t_teacher_id+
						"',[classroom_time_sections]='"+classroom_time_sections+
						"',[classroom_time_week]='"+classroom_time_week+
						"',[t_course_id]='"+t_course_id+
						"',[classroom]='"+classroom+
						"',[remark]='"+remark+
						"',[add_time]='"+add_time+
						"',[add_user]='"+add_user+
						"' where id='"+list_id+"'";
					errcode=DAO.sqlExec(sql);

					strMenu=strMenu+"(d="+d+")修改";
				}
				message=strMenu+(errcode==0?"成功":"出错")+"。";
			
			}			


			if(action=="del")
			{
				
				errcode=DAO.sqlExec("delete from t_timetable_list where id in ("+list_id+")");				
				strMenu=strMenu+"(d="+d+")删除";
				message=strMenu+(errcode==0?"成功":"出错")+"。";
			
			}			


			//Response.Write("<script>alert('"+message+"');</script>");
			if(errcode!=0) message=message+"(errcode="+errcode+")";
			if(errcode!=0) user.saveLog(Request,message); //保存日志
			string reload=errcode==0?Req.getPara(Request,"reload").Replace("{$id}",d).Replace("{$list_id}",list_id):null; //如果发生了错误，则不需要重新刷新
			Response.Write(user.getActionString(Session,errcode,reload,message)); //显示结果 
		} 
		
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
